package pers.xhh.shopapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.common.ResultCode;
import pers.xhh.shopapi.dao.MiReceiverDao;
import pers.xhh.shopapi.entity.MiReceiverEntity;
import pers.xhh.shopapi.entity.to.receiver.MiReceiverPutTo;
import pers.xhh.shopapi.service.MiReceiverService;

/**
 * @author wei-xhh
 * @date 2020-11-22
 */
@Service
public class MiReceiverServiceImpl extends ServiceImpl<MiReceiverDao, MiReceiverEntity> implements MiReceiverService {


    @Override
    public Mi updateReceiver(Integer receiverId, MiReceiverPutTo miReceiverTo) {
        if (miReceiverTo != null) {
            String receiverCity = miReceiverTo.getReceiverCity();
            String receiverDetailAddress = miReceiverTo.getReceiverDetailAddress();
            String receiverName = miReceiverTo.getReceiverName();
            String receiverPhone = miReceiverTo.getReceiverPhone();
            String receiverPostCode = miReceiverTo.getReceiverPostCode();
            String receiverProvince = miReceiverTo.getReceiverProvince();
            String receiverRegion = miReceiverTo.getReceiverRegion();

            MiReceiverEntity miReceiverEntity = new MiReceiverEntity();
            miReceiverEntity.setId(receiverId);
            if (receiverCity != null) {
                miReceiverEntity.setReceiverCity(receiverCity);
            }
            if (receiverDetailAddress != null) {
                miReceiverEntity.setReceiverDetailAddress(receiverDetailAddress);
            }
            if (receiverName != null) {
                miReceiverEntity.setReceiverName(receiverName);
            }
            if (receiverPhone != null) {
                miReceiverEntity.setReceiverPhone(receiverPhone);
            }
            if (receiverPostCode != null) {
                miReceiverEntity.setReceiverPostCode(receiverPostCode);
            }
            if (receiverProvince != null) {
                miReceiverEntity.setReceiverProvince(receiverProvince);
            }
            if (receiverRegion != null) {
                miReceiverEntity.setReceiverRegion(receiverRegion);
            }

            // ->更新
            boolean b = this.updateById(miReceiverEntity);
            if (b) {
                return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "更新成功!");
            }
            return Mi.error(ResultCode.ERROR1101.getMessage(), ResultCode.ERROR1101.getStatus());
        }
        return Mi.ok(ResultCode.ERROR1101.getMessage(), ResultCode.ERROR1101.getStatus(), "更新地址参数为空！");

    }

    @Override
    public Mi getReceiverById(Integer receiverId) {
        MiReceiverEntity byId = this.getById(receiverId);
        if(byId == null){
            return Mi.error(ResultCode.ERROR1102.getMessage(), ResultCode.ERROR1102.getStatus());
        }
        return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), byId);
    }

}
